//
//  WebController.m
//  LoveFreshBeen
//
//  Created by Cyan on 2016/10/20.
//  Copyright © 2016年 魏根. All rights reserved.
//

#import "WebController.h"

@interface WebController () <UIWebViewDelegate>


@property (nonatomic, strong) UIWebView *web;

@property (nonatomic, strong) UIView *line;

@end

@implementation WebController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self addBackButton];
    
    [self setupUI];
    
}

- (void)setupUI {
    
    self.title = self.model.name;
    
    [self.navigationController.navigationBar setBarTintColor:[UIColor whiteColor]];
    
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:self.model.toURL]];
    
    [self.web loadRequest:request];
    
    [UIView animateWithDuration:5 animations:^{
        self.line.frame = CGRectMake(0, 0, SCREEN_WIDTH * 0.7, 3);
    }];
}




- (void)addBackButton {
    
    
    
    UIButton *back = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 30, 30)];
    
    [back setBackgroundImage:[UIImage imageNamed:@"v2_goback"] forState:UIControlStateNormal];
    
    [back addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:back];
}


- (void)back {
    
    [self.navigationController popViewControllerAnimated:YES];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    
    [self.navigationController.navigationBar setBarTintColor:[UIColor colorWithRed:254/255.0 green:211/255.0 blue:65/255.0 alpha:1]];
    
}

- (UIWebView *)web {
    
    if (!_web) {
        _web = [[UIWebView alloc] init];
        
        [self.view addSubview:_web];
        
        [_web mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self.view);
        }];
        _web.delegate = self;
    }
    return _web;
    
}
- (UIView *)line {
    if (!_line) {
        _line = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 3)];
        [self.view addSubview:_line];

        _line.backgroundColor = [UIColor orangeColor];
    }
    return _line;
}


- (void)webViewDidFinishLoad:(UIWebView *)webView {
    
    [UIView animateWithDuration:0.3 delay:0.25 options:0 animations:^{
        self.line.frame = CGRectMake(0, 0, SCREEN_WIDTH, 3);
    } completion:^(BOOL finished) {
        self.line.hidden = YES;
    }];

}


@end
